package com.xiaotu.sys.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaotu.result.R;
import com.xiaotu.sys.pojo.entity.User;
import com.xiaotu.sys.service.UserService;
import com.xiaotu.utils.MD5;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaotu
 * @since 2022-01-19
 */
@CrossOrigin
@Api("用户管理")
@RestController
@RequestMapping("/sys/user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * @param pageSize 页面大小
     * @param currentPage 当前页
     * @return
     */
    @GetMapping
    public R list(Integer pageSize,Integer currentPage){
        System.out.println("页面大小"+pageSize);
        System.out.println("数据总条数"+currentPage);
        IPage<User> userPage = new Page<>(currentPage, pageSize);
        IPage<User> page = userService.page(userPage);
        return R.ok().data("list", page.getRecords()).data("total", page.getTotal());
    }
    /**
     * 更新用户信息
     * @return
     */
    @PutMapping
    public R update(User user){
        boolean update = userService.update(user, null);
        return R.ok().data("", "");
    }
    /**
     *
     * @param user
     * @return 成功状态码 20000
     *   token admin-token editor-token
     */
    @PostMapping("/login")
    public R login(@RequestBody User user){
        //根据username查询用户
        User one = userService.getOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        //拿到密码比较
        String onePassword = one.getPassword();
        //获取盐值
        String salt = one.getSalt();
        //将用户输入密码与盐值加密
        String encrypt = MD5.encrypt(user.getPassword() + salt);
        //判断密码是否正确
        if(encrypt.equalsIgnoreCase(onePassword)){
            User userAndRoles = userService.getUserAndRoles(one.getId());
            return  R.ok().code(20000).data("token",userAndRoles.getRoles().get(0).getPermission()+"-token").data("user", userAndRoles);
        }else{
            System.out.println("用户名++++"+user.getUsername());
            System.out.println("密码++++"+user.getPassword());
            return  R.error().code(50008).message("登录失败!请确认账户密码");
        }
    }
    @PostMapping("/register")
    public R register(@RequestBody User user){
        //判断用户是否存在
        User one = userService.getOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if(user.getUsername().equals(one.getUsername())){
            return R.error().message("该用户已存在!");
        }
        //密码加盐加密
        String password = user.getPassword();
        //MD5加密一次 生成盐值
        String salt = MD5.encrypt(password);
        user.setSalt(salt);
        //密码加盐值再加密一次生成存入数据库的密码
        String encrypt = MD5.encrypt(password + salt);
        user.setPassword(encrypt);
        //存入数据库
        boolean save = userService.save(user);
        return R.ok().message("注册成功!");
    }
    /**
     * 给用户赋角色权
     */
    @PostMapping("/empower")
    public R empower(String userId,String...roleIds){
        userService.empower(userId,roleIds);
        return R.ok().message("成功对用户赋予角色!");
    }
}
